local huzhen = fk.CreateSkill{
  name = "xiaobai__huzhen"
}

Fk:loadTranslationTable{
  ["xiaobai__huzhen"] = "斛珍",
  [":xiaobai__huzhen"] = "当你失去手牌后，若与你上次失去手牌的方式：不同，你可以摸一张牌；相同，你可以明置一张手牌。"..
    "你手牌因此变为均明置时，你可以用两张手牌交换一名其他角色三张手牌。",

  ["#xiaobai__huzhen_draw"] = "斛珍：你可以摸一张牌",
  ["#xiaobai__huzhen_show"] = "斛珍：你可以明置一张手牌",
  ["#xiaobai__huzhen_swap"] = "斛珍：你可以用两张手牌交换一名其他角色三张手牌",
  ["@xiaobai__huzhen_lost_reason"] = "斛珍",
  
  -- 卡牌移动原因翻译
  ["xiaobai__huzhen_reason1"] = "移动",
  ["xiaobai__huzhen_reason3"] = "弃牌",
  ["xiaobai__huzhen_reason4"] = "给予",
  ["xiaobai__huzhen_reason5"] = "置于",
  ["xiaobai__huzhen_reason6"] = "置入弃牌堆",
  ["xiaobai__huzhen_reason8"] = "交换",
  ["xiaobai__huzhen_reason9"] = "使用",
  ["xiaobai__huzhen_reason10"] = "打出",
  ["xiaobai__huzhen_reason12"] = "重铸",
}

local DIY = require "packages/diy_utility/diy_utility"

huzhen:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand then
              player.room:setPlayerMark(player, "xiaobai__huzhen_lost", move.moveReason)
              player.room:setPlayerMark(player, "@xiaobai__huzhen_lost_reason", "xiaobai__huzhen_reason" .. move.moveReason)
              return true
            end
          end
        end
      end
    end)
  end
end)

huzhen:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:getMark("xiaobai__huzhen_lost") == 0 then return end
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand then
            return true
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local resonLostCard = -1
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand then
            resonLostCard = move.moveReason
            break
          end
        end
      end
      if resonLostCard ~= -1 then break end
    end
    if resonLostCard == -1 then return end
    if player:getMark("xiaobai__huzhen_lost") ~= resonLostCard then
      event:setCostData(self, {action = 1})
      return room:askToSkillInvoke(player, {
        skill_name = huzhen.name,
        prompt = "#xiaobai__huzhen_draw"
      })
    else
      local cards = DIY.askToShowCards(player, {
        max_num = 1,
        min_num = 1,
        skill_name = huzhen.name,
        prompt = "#xiaobai__huzhen_show",
        skip = true,
        cancelable = true,
      })
      if #cards > 0 then
        event:setCostData(self, {action = 2, cards = cards})
        return true
      end
    end
  end, 
  on_use = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    local room = player.room
    if cost_data.action == 1 then
      player:drawCards(1, huzhen.name)
    elseif cost_data.action == 2 then
      DIY.showCards(player, cost_data.cards)
      if #DIY.getShownCards(player) == player:getHandcardNum() then
        local targets = table.filter(room:getOtherPlayers(player, false), function (p)
          return p:getHandcardNum() >= 3
        end)
        if #targets == 0 then return end
        local swapers,cards,done = room:askToChooseCardsAndPlayers(player, {
          max_num = 1,
          min_num = 1,
          targets = targets,
          skill_name = huzhen.name,
          prompt = "#xiaobai__huzhen_swap",
          cancelable = true,
          include_equip = false,
          max_card_num = 2,
          min_card_num = 2, 
        })
        if (not done) or #swapers == 0 or #cards == 0 then return end
        local swaper = swapers[1]
        local swpCards = room:askToChooseCards(swaper, {
          min = 3,
          max = 3,
          skill_name = huzhen.name,
          prompt = "#xiaobai__huzhen_chooseCards",
          flag = "h",
          target = swaper,
        })
        if #swpCards == 0 then return end
        room:swapCards(player, {{player, cards}, {swaper, swpCards}}, huzhen.name)
      end
    end
  end
})

huzhen:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(huzhen.name)
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand then
            player.room:setPlayerMark(player, "xiaobai__huzhen_lost", move.moveReason)
            player.room:setPlayerMark(player, "@xiaobai__huzhen_lost_reason", "xiaobai__huzhen_reason" .. move.moveReason)
            return
          end
        end
      end
    end
  end,
  late_refresh = true, 
})

return huzhen